<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:collectTime:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:collectTime:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:collectTime:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-question"
          size="mini"
          @click="handleCeshi"
          v-hasPermi="['system:collectTime:feeCeshi']"
        >费用测算</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="collectTimeList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="收费方式" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="规则时间段" align="center" prop="timeSlot" />
      <el-table-column label="结算方式" align="center" prop="settlement" :formatter="settlementFormat" />
      <el-table-column label="封顶价格" align="center" prop="fendingPrice" />
      <el-table-column label="分钟数" align="center" prop="minute" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="之后(分钟)" align="center" prop="lastMinute" />
      <el-table-column label="增加(元)" align="center" prop="lastPrice" />
      <el-table-column label="所属规则" align="center" prop="feesName" />
      <el-table-column label="提交者" align="center" prop="submitName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:collectTime:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:collectTime:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改计费规则时段对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"   label-width="120px">

        <el-form-item label="规则时间段" prop="timeSlot">
          <el-time-picker
            is-range style="width: 100%"
            v-model="form.timeSlot"
            range-separator=" - "
            start-placeholder="开始时间"
            value-format="HH:mm:ss"
            end-placeholder="结束时间"
            placeholder="选择时间范围">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="收费方式" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
          <!--按次定额收费-->
        <div v-if="form.type == 0 ">
          <el-form-item label="金额" prop="price">
            <el-input placeholder="￥" v-model="form.price">
              <template #append>元/次</template>
            </el-input>
          </el-form-item>
        </div>

        <!--按单价收费-->
        <div v-if="form.type == 1 ">
          <el-form-item label="每" prop="minute">
            <el-input placeholder="请输入分钟数" v-model="form.minute" style="width: 50%">
              <template #append>分钟</template>
            </el-input>
            <el-input placeholder="￥" v-model="form.price" style="width: 50%">
              <template #append>元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="结算方式" prop="settlement">
            <el-radio-group v-model="form.settlement">
              <el-radio
                v-for="dict in settlementOptions"
                :key="dict.dictValue"
                :label="parseInt(dict.dictValue)"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="封顶价格" prop="fendingPrice" v-if="form.settlement == 2">
            <el-input v-model="form.fendingPrice" placeholder="请输入封顶价格" >
              <template #append>元</template>
            </el-input>
          </el-form-item>
        </div>
        <!--递增收费-->
        <div v-if="form.type == 2 ">
          <el-form-item label="首" prop="minute">
            <el-input placeholder="请输入分钟数" v-model="form.minute" style="width: 50%">
              <template #append>分钟</template>
            </el-input>
            <el-input placeholder="￥" v-model="form.price" style="width: 50%">
              <template #append>元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="之后" prop="lastMinute">
            <el-input placeholder="分钟数" v-model="form.lastMinute" style="width: 50%">
              <template #append>分钟</template>
            </el-input>
            <el-input placeholder="￥" v-model="form.lastPrice" style="width: 50%">
              <template #append>元</template>
            </el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 费用测算 -->
    <el-dialog :title="title" :visible.sync="openCeshi" width="30%" append-to-body>
      <el-form ref="formCeshi" :model="formCeshi" :rules="rulesCeshi"   label-width="120px">
        <el-form-item label="进场时间" >
          <el-date-picker
            style="width: 100%;"
            clearable size="small"
            v-model="formCeshi.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择进场时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出场时间" >
          <el-date-picker
            style="width: 100%;"
            clearable size="small"
            v-model="formCeshi.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择出场时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预算价格" prop="totalPrice">
          <el-input placeholder="￥" v-model="totalPrice" readonly>
            <template #append>元</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitFormCeshi">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCollectTime, getCollectTime, delCollectTime, addCollectTime, updateCollectTime, exportCollectTime ,ceshiCollectTime} from "@/api/system/collectTime";

export default {
  name: "CollectTime",
  components: {
  },
  props: {
    feesId: {
      type: Number,
      default:function(){
        return undefined;
      }
    }
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 计费规则时段表格数据
      collectTimeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 费用测算是否显示弹出层
      openCeshi: false,
      // 收费方式字典
      typeOptions: [],
      // 结算方式字典
      settlementOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        feesId: this.feesId,
      },
      // 表单参数
      form: {},
      // 费用测算表单参数
      formCeshi: {},
      totalPrice: 0,
      // 表单校验
      rules: {
        timeSlot:[
          {required:true,message:"请选择时间段",trigger:"blur"}
        ],
        type:[
          {required:true,message:"请选择收费方式",trigger:"blur"}
        ],
        price:[
          {required:true,message:"请填写价格",trigger:"blur"}
        ],
      },
      // 费用测算表单校验
      rulesCeshi: {
        startTime:[
          {required:true,message:"请选择进场时间",trigger:"blur"}
        ],
        endTime:[
          {required:true,message:"请选择出场时间",trigger:"blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("collect_time_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("collect_settlement").then(response => {
      this.settlementOptions = response.data;
    });
  },
  methods: {
    /** 查询计费规则时段列表 */
    getList() {
      this.loading = true;
      listCollectTime(this.queryParams).then(response => {
        this.collectTimeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 收费方式字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 结算方式字典翻译
    settlementFormat(row, column) {
      return this.selectDictLabel(this.settlementOptions, row.settlement);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openCeshi = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        type: undefined,
        timeSlot: undefined,
        settlement: undefined,
        price: undefined,
        submitName: undefined,
        fendingPrice: undefined,
        minute: undefined,
        lastPrice: undefined,
        lastMinute: undefined,
        feesId: undefined,
        createtime: undefined,
        updatetime: undefined
      };
      this.formCeshi = {
        feesId: undefined,
        startTime: undefined,
        endTime: undefined
      };
      this.resetForm("form");
      this.resetForm("formCeshi");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加计费规则时段";
    },
    /** 费用测算 */
    handleCeshi() {
      this.reset();
      this.openCeshi = true;
      this.title = "费用测算";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getCollectTime(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改计费规则时段";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // this.buttonLoading = true;
          this.form.feesId = this.feesId;
          console.log(this.form)
          // this.form.timeSlot = this.form.timeSlot.join(" - ");
          if (this.form.type == 0 ){  //按次定额收费
            this.form.minute = 0 ;
            this.form.lastMinute = 0 ;
            this.form.fendingPrice = 0 ;
            this.form.lastPrice = 0 ;
          }
          if (this.form.type == 1  && this.form.settlement != 2){  //按次定额收费
            this.form.fendingPrice = 0 ;
            this.form.lastMinute = 0 ;
            this.form.lastPrice = 0 ;
          }
          if (this.form.type == 2){  //递增结算
            this.form.fendingPrice = 0 ;
          }
          if (this.form.id != null) {
            updateCollectTime(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCollectTime(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 费用测算提交按钮 */
    submitFormCeshi() {
      this.$refs["formCeshi"].validate(valid => {
        if (valid) {
          if (this.formCeshi.startTime == null || this.formCeshi.endTime == null){
            this.msgError("请选择正确的时间段");
            return false;
          }
          if (this.compareTime(this.formCeshi.startTime,this.formCeshi.endTime) == false){
            this.msgError("请选择正确的时间段");
            return false;
          }
          this.formCeshi.feesId = this.feesId;
          // console.log(this.formCeshi)
          ceshiCollectTime(this.formCeshi).then(response => {
            this.totalPrice = response.msg;
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除计费规则时段编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delCollectTime(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有计费规则时段数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCollectTime(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
